<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    #audio_canvas {
      position: relative;
      width: 100%;
      height: 50px;
      background: yellow;
    }
  </style>
</head>

<body>
  <button id="record_audio">录音频</button>
  <button id="stop_audio">stop</button>
  <audio controls="controls" id="record_play"></audio>
  <canvas id="audio_canvas"></canvas>
  <script>
    var record_audio = document.querySelector('#record_audio');
    var stop_audio = document.querySelector('#stop_audio');
    var record_play = document.querySelector('#record_play');
    var audio_stream;
    const audioCanvas = document.getElementById('audio_canvas');
    if (audioCanvas) {
      var width = audioCanvas.width,
        height = audioCanvas.height;
      var g = audioCanvas.getContext("2d");
    }

    g.lineWidth = 1;
    g.translate(0.5, height / 2 + 0.5);
    var media1 = navigator.mediaDevices.getUserMedia({
      audio: true
    });
    media1.then((stream) => {
      audio_stream = stream;
      var AudioContext = window["AudioContext"] || window["webkitAudioContext"] || window["mozAudioContext"] ||
        window["msAudioContext"];
      var context = new AudioContext();
      //从元素创建媒体节点
      var media = context.createMediaStreamSource(stream);
      //创建脚本处理节点
      var processor = context.createScriptProcessor(4096, 1, 1);
      //连接：媒体节点→控制节点→输出源
      media.connect(processor);
      processor.connect(context.destination);
      //控制节点的过程处理
      processor.onaudioprocess = function (e) {
        //获取输入和输出的数据缓冲区
        var input = e.inputBuffer.getChannelData(0);
        var output = e.outputBuffer.getChannelData(0);
        // console.log(e.outputBuffer.numberOfChannels)
        //将输入数缓冲复制到输出缓冲上
        for (var i = 0; i < input.length; i++) output[i] = input[i];
        // output [-1,1]
        // console.log(output)


        //将缓冲区的数据绘制到Canvas上
        g.clearRect(-0.5, -height / 2 - 0.5, width, height);
        g.beginPath();
        for (var i = 0; i < width; i++)
          g.lineTo(i, height / 2 * output[output.length * i / width | 0] * 5);
        g.stroke();


      };
    }).catch((error) => {
      alert(error)
    });
    record_audio.onclick = function () {
      record_audio.disabled = 'true';
      var mediaRecorder = new MediaRecorder(audio_stream);
      mediaRecorder.ondataavailable = function (e) {
        // blob_audio = new Blob([e.data], {type:e.data.type});
        blob_audio = new Blob([e.data], {
          type: 'audio/mp3'
        });
        //可以存为mp3格式但是本地无法播放，只有网页可以播放
      }
      mediaRecorder.start();
      mediaRecorder.onstop = function () {
        record_play.src = URL.createObjectURL(blob_audio);
        record_play.play();
        //download(blob_audio);
      }
      stop_audio.onclick = function () {
        record_audio.disabled = '';
        mediaRecorder.stop();
      }
    }
  </script>
</body>

</html>